"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ParamsParam = exports.HeaderParam = exports.CookieParam = exports.BodyParam = exports.QueryParam = exports.Next = exports.Params = exports.Header = exports.Cookie = exports.Body = exports.Query = exports.Response = exports.Request = exports.MethodParamFactory = exports.MethodContextFactory = exports.getParamMeta = exports.CustomDelete = exports.CustomPut = exports.CustomPost = exports.CustomGet = exports.Delete = exports.Put = exports.Post = exports.Get = exports.MethodFactory = exports.Controller = exports.getMethodMeta = exports.getMeta = exports.DataFormat = void 0;
// 数据格式
var DataFormat;
(function (DataFormat) {
    DataFormat["X_WWW_FORM_URLENCODED"] = "application/x-www-form-urlencoded";
    DataFormat["FORMDATA"] = "multipart/form-data";
    DataFormat["FORMDATA_CUSTOM"] = "multipart/form-data ";
    DataFormat["JSON"] = "application/json";
    DataFormat["PLAIN"] = "text/plain";
})(DataFormat = exports.DataFormat || (exports.DataFormat = {}));
function getMeta(constructor) {
    return constructor.prototype.$META || (constructor.prototype.$META = {
        baseUrl: '',
        routes: {},
        midwares: []
    });
}
exports.getMeta = getMeta;
function getMethodMeta(target, methodName) {
    var meta = getMeta(target.constructor);
    var routes = meta.routes;
    return routes[methodName] || (routes[methodName] = {
        subUrl: '',
        httpMethod: '',
        midwares: [],
        params: [],
        isCustom: false,
        dataFormat: DataFormat.JSON
    });
}
exports.getMethodMeta = getMethodMeta;
function Controller(baseUrl) {
    var midwares = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        midwares[_i - 1] = arguments[_i];
    }
    if (!baseUrl.startsWith('/')) {
        throw new Error('请输入 / 开头的路径');
    }
    return function (constructor) {
        var meta = getMeta(constructor);
        meta.baseUrl = baseUrl;
        meta.midwares = midwares;
    };
}
exports.Controller = Controller;
function MethodFactory(httpMethod, isCustom) {
    if (isCustom === void 0) { isCustom = false; }
    return function (subUrl, temp) {
        var midwares = [];
        for (var _i = 2; _i < arguments.length; _i++) {
            midwares[_i - 2] = arguments[_i];
        }
        if (!subUrl.startsWith('/')) {
            throw new Error('请输入 / 开头的路径');
        }
        var dataFormat = DataFormat.JSON;
        if (temp && Object.values(DataFormat).includes(temp)) {
            dataFormat = temp;
        }
        else if (temp) {
            midwares.unshift(temp);
        }
        return function (target, methodName) {
            var mehtodMeta = getMethodMeta(target, methodName);
            mehtodMeta.subUrl = subUrl;
            mehtodMeta.httpMethod = httpMethod;
            mehtodMeta.midwares = midwares;
            mehtodMeta.isCustom = isCustom;
            mehtodMeta.dataFormat = dataFormat;
        };
    };
}
exports.MethodFactory = MethodFactory;
exports.Get = MethodFactory('get');
exports.Post = MethodFactory('post');
exports.Put = MethodFactory('put');
exports.Delete = MethodFactory('delete');
exports.CustomGet = MethodFactory('get', true);
exports.CustomPost = MethodFactory('post', true);
exports.CustomPut = MethodFactory('put', true);
exports.CustomDelete = MethodFactory('delete', true);
function getParamMeta(target, methodName, paramIndex) {
    var mehtodMeta = getMethodMeta(target, methodName);
    return mehtodMeta.params[paramIndex] || (mehtodMeta.params[paramIndex] = {
        paramIndex: paramIndex,
        paramType: 'request',
        paramName: ''
    });
}
exports.getParamMeta = getParamMeta;
function MethodContextFactory(paramType) {
    return function (target, methodName, paramIndex) {
        var paramMeta = getParamMeta(target, methodName, paramIndex);
        paramMeta.paramType = paramType;
    };
}
exports.MethodContextFactory = MethodContextFactory;
function MethodParamFactory(paramType) {
    return function (paramName) {
        return function (target, methodName, paramIndex) {
            var paramMeta = getParamMeta(target, methodName, paramIndex);
            paramMeta.paramType = paramType;
            paramMeta.paramName = paramName;
        };
    };
}
exports.MethodParamFactory = MethodParamFactory;
exports.Request = MethodContextFactory('request');
exports.Response = MethodContextFactory('response');
exports.Query = MethodContextFactory('query');
exports.Body = MethodContextFactory('body');
exports.Cookie = MethodContextFactory('cookie');
exports.Header = MethodContextFactory('header');
exports.Params = MethodContextFactory('params');
exports.Next = MethodContextFactory('next');
exports.QueryParam = MethodParamFactory('query');
exports.BodyParam = MethodParamFactory('body');
exports.CookieParam = MethodParamFactory('cookie');
exports.HeaderParam = MethodParamFactory('header');
exports.ParamsParam = MethodParamFactory('params');
